package org.gc.webapp.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.displaytag.tags.TableTagParameters;
import org.gc.Constants;
import org.gc.common.entity.security.User;
import org.gc.common.vo.naturalZone.NaturalZoneVO;
import org.gc.service.naturalZone.NaturalZoneService;
import org.gc.util.comparator.NaturalZoneVOComparator;
import org.gc.webapp.util.page.BasePageSO;
import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;



public class BizUtil {
    /**
     * 获取当前登录的用户名
     * @return
     *          如果用户登录了，就返回登录的用户名；否则返回null
     */
    static public String getUsername(){
        String result = null;
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth == null){
            return result;
        }
        Object o = auth.getPrincipal();
        if(o != null && o instanceof User){
            result = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
        }else{
            result = o.toString();
        }
        return result;
    }
    /**
     * 获取当前登录的用户对象
     * @return
     *          如果用户登录了，就返回登录的用户；否则返回null
     */
    static public User getLoginUser(){
        User result = null;
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if(auth == null){
            return result;
        }
        Object o = auth.getPrincipal();
        if(o != null && o instanceof User){
            result = (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }else{
            result = null;
        }
        return result;
    }
    
    /**
     * 获取所有省的id-name map
     * @param naturalZoneService
     * @return
     */
    static public Map<String, String> getProvinceMap(NaturalZoneService naturalZoneService){
        Map<String, String> result = new TreeMap<String, String>();
        List<NaturalZoneVO> children = naturalZoneService.getDirectChildren(1001L);
        Collections.sort(children, new NaturalZoneVOComparator());
        for(NaturalZoneVO naturalZoneVO : children){
            result.put(naturalZoneVO.getId()+ "", naturalZoneVO.getName());
        }
        return result;
    }
    /**
     * 获取通用的操作列表（包括编辑和删除按钮）
     * @param entityName
     *          实体类名
     */
    static public String getGeneralOperation(Long id){
        StringBuffer sb = new StringBuffer();
        //String lowerEntityName = entityName.substring(0, 1).toLowerCase();
        /*if(entityName.length() > 1){
            lowerEntityName += entityName.substring(1);
        }*/
        sb.append("<a href='edit.do?id=");
        sb.append(id);
        sb.append("'>");
        sb.append("编辑");
        sb.append("</a>");
        sb.append(" ");
        sb.append("<a onclick='return window.confirm(\"您确定要删除吗？\");' href='delete" + ".do?" + "id=");
        sb.append(id);
        sb.append("'>");
        sb.append("删除");
        sb.append("</a>");
        sb.append(" ");
        return sb.toString();
    }
    /**
     * 获取缺省的省市区id列表
     * @return
     */
    static public List<String> getDefaultAddressArea(){
        List<String> result = new ArrayList<String>(3);
        result.add("22836");
        result.add("22837");
        result.add("22878");
        
        return result;
    }
    /**
     * 准备导出数据（导出excel，pdf...）时使用的翻页参数
     * @param request
     * @param so
     */
    static public void prepareExport(HttpServletRequest request, BasePageSO so){
        if(request.getParameter(TableTagParameters.PARAMETER_EXPORTING) != null){
            so.setPageNumber(1);
            so.setObjectsPerPage(Constants.MAX_EXPORT_SIZE);
        }
    }    
}
